package com.wty.shop.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author wangtengyu
 * @Create 2018-02-01-14:15
 */
@Slf4j
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        log.error("{} Exception",httpServletRequest.getRequestURI(),e);

        //当使用是jackson2.x的时候使用MappingJackson2JsonView，课程中使用的是1.9
        ModelAndView modelAndView=new ModelAndView(new MappingJackson2JsonView());

        modelAndView.addObject("statsu",ResponseCode.ERROR.getCode());
        modelAndView.addObject("msg","接口异常，请查看服务端日志的详细情况");
        modelAndView.addObject("data",e);
        return modelAndView;
    }
}
